<template>
  <!-- 搜索药品 -->
  <el-popover
    ref="popover"
    v-model="dialogShow"
    placement="bottom-start"
    width="700"
    trigger="click"
  >
    <el-input
      v-model="searchName"
      slot="reference"
      style="width:100%"
      placeholder="选择药品"
      @input="onSearchData"
    ></el-input>
    <br />
    <div ref="element-box" style="height:350px;overflow-y: auto;">
      <table class="table-box">
        <thead>
          <tr>
            <th>药品名</th>
            <th>规格</th>
            <th>生产厂家</th>
            <th>库存</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in tableList" :key="index" @click="onGetDetail(item)">
            <td>{{ item.drugName }}</td>
            <td>
              <span v-if="type != 2">{{ item.specs }}</span>
              <span v-else>
                <span v-if="item.medicalDrugSale">
                  {{
                  item.medicalDrugSale.packUnit | changeAssist(unitEnum)
                  }}
                </span>
              </span>
            </td>
            <td>{{ item.factory }}</td>
            <td>{{ item.inventoryStatisticVO | durgInventory(unitEnum) }}</td>
          </tr>
        </tbody>
      </table>
      <br />
      <pager v-model="pager" @change-pagenum="getData"></pager>
    </div>
  </el-popover>
</template>
<script>
export default {
  props: ["value", "patientId", "doctorId", "type", "channel"],
  components: {},
  computed: {
    unitEnum() {
      return this.$store.state.doctorMZ.enum.doseUnitEnum;
    }
  },
  data() {
    return {
      searchName: "",
      dialogShow: false,
      tableList: [],
      pager: { pageSize: 5, pageNum: 1, total: 0 },
      time: null
    };
  },
  created() {},
  mounted() {
    this.getData();
  },
  methods: {
    //搜索药品
    onSearchData() {
      let th = this;
      clearTimeout(th.time);
      th.time = setTimeout(() => {
        th.pager.pageNum = 1;
        th.getData();
      }, 500);
    },
    getData() {
      let th = this;
      let j = {
        type: th.type || 1,
        queryStr: th.searchName,
        status: 1
      };
      if (this.channel) {
        j.channel = this.channel;
      }
      Object.assign(j, th.pager);
      th.$api.pageQueryMedicalDrugInfo(j).then(res => {
        if (res.executed) {
          th.tableList = res.dataBody.list;
          th.pager.total = res.dataBody.total;
        }
      });
    },
    //查询药品用法详情
    onGetDetail(obj) {
      let th = this;
      let j = {
        drugName: obj.drugName,
        sellMoney: 0,
        prescriptionId: "",
        hpId: th.adminUser.hpId,
        patientId: th.patientId,
        deptId: th.adminUser.hpId,
        doctorId: th.doctorId,
        drugId: obj.id,
        groupId: "",
        oneDose: obj.oneDose,
        oneDoseUnit: obj.oneDoseUnit,
        use: obj.defaultUse,
        frequency: obj.defaultFrequency,
        useDay: "",
        useTotal: "",
        totalUnit: obj.packUnit,
        getAddress: "",
        id: "",
        isPiece: obj.isPiece,
        remark: obj.remark,
        specs: obj.specs,
        highWarning:obj.highWarning,//是否高危药品
        antibacterial:obj.antibacterial,//是否抗菌药

        medicalDrugSale: obj.medicalDrugSale,
        inventory: obj.inventoryStatisticVO ? obj.inventoryStatisticVO : {}
      };
      if (obj.medicalDrugSale) {
        j.getAddress = obj.medicalDrugSale.takeAddress;
      }
      // if (obj.inventoryStatisticVO) {
      //   j.useTotal =
      //     th.type == 1 ? obj.inventoryStatisticVO.packUnit : obj.oneDoseUnit;
      // }
      th.$emit("addItem", j);
      th.dialogShow = false;
    }
  }
};
</script>
